#pragma once
#include "HttpSession.h"
#include "GoogleException.h"

#include "GoogleAuth.h"

using namespace HttpProtocol;

//200 OK No error. 
//201 CREATED Creation of a resource was successful. 
//304 NOT MODIFIED The resource hasn't changed since the time specified in the request's If-Modified-Since header. 
//400 BAD REQUEST Invalid request URI or header, or unsupported nonstandard parameter. 
//401 UNAUTHORIZED Authorization required. 
//403 FORBIDDEN Unsupported standard parameter, or authentication or authorization failed. 
//404 NOT FOUND Resource (such as a feed or entry) not found. 
//409 CONFLICT Specified version number doesn't match resource's latest version number. 
//410 GONE Requested change history is no longer available on the server. Refer to service-specific documentation for more details. 
//500 INTERNAL SERVER ERROR Internal error. This is the default code that is used for all unrecognized errors. 

#define G_RET     LRESULT
#define G_OK            1
#define G_FAILED        0
#define G_HTTP_ERROR   -1
#define G_AUTH_ERROR   -2
#define G_PWD_ERROR    -3

namespace GoogleProtocol
{

class CGoogleProtocol
{
public:
	CGoogleProtocol(void);
	~CGoogleProtocol(void);

protected:
	BOOL IsErrorRequest(DWORD dwStatus);
	bool ParseContact(LPCSTR lpRecv);
	bool ParseAuthToken(LPCSTR lpRecv);
	DWORD GetLastRequestCode(void);

public:
	G_RET Logon(LPCSTR lpEmail, LPCSTR lpPassword, CGoogleAuth* outAuth);
	BOOL LogOut(void);

protected:
	BOOL IsLogonFailed(void);
	BOOL IsCreateSucceed(void);
	BOOL IsDeleteSucceed(void);
	BOOL IsUpdateSucceed(void);

public:
	BOOL ReadStream(LPSTR lpOutBuffer, DWORD& bufsize);
	bool SendGetRequest(void);

protected:
	// test bit, our chttp opration streams.
	CHttpSession* m_pHttpSession;
	CHttpConnect* m_pWebConn;
	CHttpStream* m_pHttpFile;
};

}